﻿using StudentInfoSystem.Core.Interfaces;
using StudentInfoSystem.Core.Models;
using System.Collections.Generic;
using System.Linq;

namespace StudentInfoSystem.Core.MockImplementations
{
    /// <summary>
    /// 统计服务的模拟实现，提供学生数据的统计分析功能
    /// 支持按学院和学年进行数据过滤
    /// </summary>
    public class MockStatisticsService : IStatisticsService
    {
        private readonly IStudentService _studentService;

        /// <summary>
        /// 初始化统计服务
        /// </summary>
        /// <param name="studentService">学生服务实例，用于获取学生数据</param>
        public MockStatisticsService(IStudentService studentService)
        {
            _studentService = studentService;
        }

        /// <summary>
        /// 获取学院分布统计
        /// </summary>
        /// <param name="college">可选，按学院过滤</param>
        /// <param name="academicYear">可选，按学年过滤(格式: yyyy)</param>
        /// <returns>学院名称->学生数量的字典</returns>
        public Dictionary<string, int> GetCollegeDistribution(string? college = null, string? academicYear = null)
        {
            return GetDistribution(s => s.College, college, academicYear);
        }

        /// <summary>
        /// 获取院系列表分布统计
        /// </summary>
        /// <param name="college">可选，按学院过滤</param>
        /// <param name="academicYear">可选，按学年过滤(格式: yyyy)</param>
        /// <returns>院系名称->学生数量的字典</returns>
        public Dictionary<string, int> GetDepartmentDistribution(string? college = null, string? academicYear = null)
        {
            return GetDistribution(s => s.Department, college, academicYear);
        }

        /// <summary>
        /// 获取班级分布统计
        /// </summary>
        /// <param name="college">可选，按学院过滤</param>
        /// <param name="academicYear">可选，按学年过滤(格式: yyyy)</param>
        /// <returns>班级名称->学生数量的字典</returns>
        public Dictionary<string, int> GetClassDistribution(string? college = null, string? academicYear = null)
        {
            return GetDistribution(s => s.Class, college, academicYear);
        }

        /// <summary>
        /// 获取生源地分布统计
        /// </summary>
        /// <param name="college">可选，按学院过滤</param>
        /// <param name="academicYear">可选，按学年过滤(格式: yyyy)</param>
        /// <returns>生源地->学生数量的字典</returns>
        public Dictionary<string, int> GetHometownDistribution(string? college = null, string? academicYear = null)
        {
            return GetDistribution(s => s.Hometown, college, academicYear);
        }

        /// <summary>
        /// 通用分布统计方法
        /// </summary>
        /// <param name="selector">分组选择器</param>
        /// <param name="college">学院过滤条件</param>
        /// <param name="academicYear">学年过滤条件</param>
        /// <returns>分组名称->数量的字典</returns>
        private Dictionary<string, int> GetDistribution(
            Func<Student, string> selector,
            string? college = null,
            string? academicYear = null)
        {
            // 获取基础数据集（使用一级缓存原始数据）
            IEnumerable<Student> students = _studentService.GetAll();

            // 应用学院过滤
            if (!string.IsNullOrEmpty(college))
            {
                students = students.Where(s => s.College == college);
            }

            // 应用学年过滤（通过学号前4位判断）
            if (!string.IsNullOrEmpty(academicYear))
            {
                students = students.Where(s => s.Id.StartsWith(academicYear));
            }

            // 分组统计
            return students
                .GroupBy(selector)
                .ToDictionary(g => g.Key, g => g.Count());
        }
    }
}